{# table #}
from fastapi import APIRouter, Depends
from sqlmodel import Session
from model import {{table | to_pascal}}DTO, {{table | to_pascal}}, Result, PageResult, {{table | to_pascal}}Query
from db import engine

{{table | to_snake}}_router = APIRouter(prefix="/{{table | to_pascal}}", tags=["{{table | to_pascal}}"])

@{{table | to_snake}}_router.get("/{id}", summary="通过ID查询详情")
def query_{{table | to_snake}}_by_id(id: int) -> Result[{{table | to_pascal}}]:
    with Session(engine) as session:
        return Result.ok({{table | to_pascal}}.query_by_id(session, id))

@{{table | to_snake}}_router.get("", summary="分页条件查询")
def query_{{table | to_snake}}_all_by_limit(query: {{table | to_pascal}}Query = Depends()) -> PageResult[{{table | to_pascal}}]:
    with Session(engine) as session:
        total = {{table | to_pascal}}.count(session, **query.count_kwargs(exclude_none=True))
        data = {{table | to_pascal}}.query_all_by_limit(session, **query.model_dump(exclude_none=True))
        return PageResult.ok(data=data, total=total)


@{{table | to_snake}}_router.post("", summary="新增数据")
def create_{{table | to_snake}}(instance: {{table | to_pascal}}DTO) -> Result[{{table | to_pascal}}]:
    with Session(engine) as session:
        return Result.ok({{table | to_pascal}}.create(session, instance))


@{{table | to_snake}}_router.patch("/{id}", summary="更新数据")
def update_{{table | to_snake}}_by_id(id: int, instance: {{table | to_pascal}}DTO) -> Result[{{table | to_pascal}}]:
    with Session(engine) as session:
        return Result.ok({{table | to_pascal}}.update(session, id, instance))


@{{table | to_snake}}_router.delete("/{id}", summary="删除数据")
def delete_{{table | to_snake}}_by_id(id: int) -> Result[{{table | to_pascal}}]:
    with Session(engine) as session:
        return Result.ok({{table | to_pascal}}.delete_by_id(session, id))